home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 00 / 5 / DISK0050.ZIP / ROFF2.C < prev    next >
Text File  |  1982-09-01  |  9KB  |  428 lines

  1. /* 7 MAY 81 */
  2.  
  3. #include "stdio.h"
  4. #include "ctype.h"
  5. #include "roff.he"
  6.  
  7. /****************************************************************
  8. handles case of leading blanks or tabs; empty lines 
  9. ******************************************************************/
  10.  
  11. leadbl (line)
  12. char *line;
  13. {
  14.     int i, j;
  15.  
  16.  
  17.     brk();
  18.  
  19.     /* find first non-blank */
  20.     for( i=0; line[i] == BLANK; i++ );
  21.  
  22.     if ( line[i] != NEWLINE ) 
  23.         TIVAL = i;
  24.     while (line[i] == TAB)
  25.     { TIVAL += 8;
  26.         i++;
  27.     }
  28.  
  29.     /* move line to left */
  30.     for (j=0; (line[j] = line[i]) != '\0'; j++, i++ );
  31.  
  32.  
  33.     return;
  34. }
  35.  
  36.  
  37.  
  38. /*******************************************************************
  39. get indices of first and last occurrences of c in string
  40. ********************************************************************/
  41. int find_char (string, c, first, last)
  42. char *string, c;
  43. int *first, *last;
  44. {
  45.     int i, j, k;
  46.  
  47.     *first = -1;
  48.     *last = -1;
  49.     for (i=0; string[i] != '\0'; i++)
  50.     {
  51.         if (string[i] == c)
  52.         { *last = i;
  53.             if (*first == -1)    *first = i;
  54.         }
  55.     }
  56.     return;
  57. }
  58.  
  59. /***************************************************************
  60.     replace c1 in string with c2
  61. ****************************************************************/
  62. replace_char (string, c1, c2)
  63. char *string, c1, c2;
  64. {
  65.     int i;
  66.  
  67.     for (i=0; string[i] != '\0'; i++)
  68.         if (string[i] == c1)    string[i] = c2;
  69.  
  70.     return;
  71. }
  72.  
  73.  
  74. /********************************************************************
  75.         produces n empty lines
  76. *********************************************************************/
  77. skip (n)
  78. int n;
  79. {
  80.     int i;
  81.  
  82.     if DEBUG fprintf(stderr,"\n    SKIP %d line(s)", n);
  83.  
  84.     for ( i=0; i<n; i++)
  85.     { if DEBUG putchar('!');
  86.         putchar(NEWLINE);
  87.     }
  88.  
  89.     return;
  90. }
  91.  
  92.  
  93. /******************************************************************
  94.     indents the proper number of spaces
  95. *******************************************************************/
  96. indent(val)
  97. int val;
  98. {
  99.     int i;
  100.  
  101.     if DEBUG fprintf(stderr,"\n    INDENT %d spaces(s)",val);
  102.  
  103.     for ( i=0; i<val; i++ )        putchar( BLANK );
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110. /*******************************************************************
  111.         puts out page header
  112. *******************************************************************/
  113. phead()
  114. {
  115.  
  116.  
  117.     CURPAG = NEWPAG;
  118.     NEWPAG++;
  119.  
  120.     if ( M1VAL > 0 )
  121.     { skip ( M1VAL - 1 );
  122.         puttl ( HEADER, CURPAG );
  123.     }
  124.  
  125.     skip ( M2VAL );
  126.  
  127.     LINENO = M1VAL + M2VAL + 1;
  128.     if DEBUG fprintf(stderr,"\nLINENO=%d", LINENO);
  129.     return;
  130. }
  131.  
  132.  
  133. /*********************************************************************
  134.         puts out page footer
  135. *********************************************************************/
  136. pfoot()
  137. {
  138.  
  139.  
  140.     skip ( M3VAL );
  141.     if ( M4VAL > 0 )
  142.     { puttl ( FOOTER, CURPAG );
  143.         skip ( M4VAL - 1 );
  144.     }
  145.     return;
  146.  
  147. }
  148.  
  149.  
  150. /*******************************************************************
  151.     put out title line with optional page no.
  152. *******************************************************************/
  153. puttl ( title_str, pageno )
  154. char *title_str;
  155. int pageno;
  156. {
  157.     int i;
  158.  
  159.  
  160.     for ( i=0; title_str[i] != '\0'; i++ )
  161.         if ( title_str[i] == NUMSIGN )
  162.             putdec ( pageno, 1 );    /* print pageno, width >= 1 */
  163.     else
  164.         putchar( title_str[i]);
  165.  
  166.     return;
  167. }
  168.  
  169. /*******************************************************************
  170.     put out num in string of width >= w
  171. ******************************************************************/
  172. putdec ( num, w )
  173. int num;
  174. int w;
  175. {
  176.     int i, nd;
  177.     char chars[10];
  178.  
  179.  
  180.     nd = itoc ( num, chars, 10 );
  181.     for ( i=nd + 1; i<=w; i++ )
  182.         putchar(BLANK);
  183.     for ( i=0;i<=nd; i++)
  184.         putchar( chars[i] );
  185.  
  186.     return;
  187. }
  188.  
  189. /*******************************************************************
  190.     convert int num to char string in numstr
  191. *********************************************************************/
  192. itoc ( num, numstr, size )
  193. int num;
  194. char *numstr;
  195. int size;    /* largest size of numstr */
  196. {
  197.     int absnum, i, j, k, d;
  198.  
  199.  
  200.     absnum = abs(num);
  201.     numstr[0] = '\0';
  202.     i = 0;
  203.  
  204.     do
  205.     { i++;
  206.         d = absnum % 10;
  207.         numstr[i] = d + '0';
  208.         absnum = absnum/10;
  209.     } while ( absnum != 0 && i<size );
  210.  
  211.     if ( num < 0 && i<size )
  212.     { i++;
  213.         numstr[i] = '-';
  214.     }
  215.  
  216.     for ( j=0; j<i; j++ )
  217.     { k = numstr[i];
  218.         numstr[i] = numstr[j];
  219.         numstr[j] = k;
  220.         i--;
  221.     }
  222.  
  223.     return ( strlen(numstr) );
  224. }
  225.  
  226. /********************************************************************
  227.     copy title from com_line to ttl
  228. **********************************************************************/
  229. gettl ( com_line, ttl )
  230. char *com_line, *ttl;
  231. {
  232.     int i;
  233.     char local[ MAXLINE ];
  234.  
  235.  
  236.     if DEBUG fprintf(stderr,"\n\nGETTL command line= <%s>", com_line);
  237.  
  238.     i=0;
  239.     while ( com_line[i]!= ' ' && com_line[i]!='\t' && com_line[i]!='\n')
  240.         i++;
  241.  
  242.     strcpy ( local, com_line );
  243.     skip_blanks (&local[i]);
  244.  
  245.     /* strip quote if found */
  246.     if ( local[i]==SQUOTE || local[i]==DQUOTE)    i++;
  247.  
  248.     strcpy ( ttl, &local[i] );
  249.     if DEBUG fprintf(stderr,"\ntitle = <%s>", ttl);
  250.  
  251.     return;
  252. }
  253.  
  254.  
  255. /******************************************************************
  256.     space n lines or to bottom of the page
  257. *******************************************************************/
  258. space (n)
  259. int n;
  260. {
  261.  
  262.     if DEBUG fprintf(stderr,"\nSPACE %d line(s), LINENO= %d", n, LINENO);
  263.  
  264.     brk();    /* flush out last unfilled line */
  265.     if (LINENO > BOTTOM)    return;    /* end of page */
  266.  
  267.     if ( LINENO == 0 )    /* top of page */
  268.         phead();
  269.  
  270.     skip( min( n, BOTTOM+1-LINENO ));    /* can't skip past bottom  */
  271.     LINENO = LINENO + n;    /* obvious */
  272.  
  273.     if DEBUG fprintf(stderr,"\n    LINENO = %d", LINENO);
  274.     if (LINENO > BOTTOM)
  275.     {
  276.         pfoot();    /* print footer if bottom */
  277.     }
  278.  
  279.     return;
  280. }
  281.  
  282.  
  283. /*******************************************************
  284.     yet ANOTHER version of text !!! no. 59,999,999
  285. get it right this time Kath dear
  286. *******************************************************/
  287. text (line)
  288. char *line;
  289. {
  290.     char wrdbuf [MAXLINE];
  291.     int i, j, k;
  292.     char *p1, *p2;
  293.  
  294.     if DEBUG fprintf(stderr,"\n\nTEXT:<%s>", line);
  295.     if (line[0] == BLANK || line[0] == NEWLINE || line[0] == TAB)
  296.         leadbl (line);
  297.     if (ULVAL > 0)    /* set high bits of all non-white space chars */
  298.     {
  299.         ULVAL--;
  300.         p1 = p2 = line;
  301.         while (*p2)
  302.         { if (*p2 == TAB || *p2 == BLANK || *p2 == NEWLINE)
  303.                 *p1++ = *p2++;
  304.             else 
  305.                 *p1++ = *p2++ | 0x80;
  306.         }
  307.     }
  308.     if (CEVAL > 0)
  309.     { center (line);
  310.         put (line);
  311.         CEVAL--;
  312.     }
  313.     else if ( line[0] == NEWLINE || FILL == NO )
  314.         put (line);
  315.     else while (WE_HAVE_A_WORD == getwrd (line, wrdbuf))
  316.             putwrd (wrdbuf);
  317.     return;
  318. }
  319.  
  320. /***********************************************************
  321.     put out a line of text with correct indentation
  322.     underlining if specified
  323. ************************************************************/
  324. put (line)
  325. char *line;
  326. {
  327.     int i, j, k;
  328.  
  329.     if DEBUG fprintf(stderr,"\n\nPUT<%s>",line);
  330.     if (LINENO == 0 || LINENO > BOTTOM )    phead();
  331.     indent (TIVAL);
  332.     putline (line);
  333.     TIVAL = INVAL;
  334.     skip (min(LSVAL-1, BOTTOM-LINENO));
  335.     LINENO = LINENO + LSVAL;
  336.     if DEBUG fprintf(stderr,"\nLINENO=%d,  LSVAL=%d",LINENO, LSVAL);
  337.     if (LINENO > BOTTOM)    pfoot();
  338.     return;
  339. }
  340. /***********************************************************
  341. concatenates the word onto the end of OUTBUF for filled text
  342. ************************************************************/
  343. putwrd (wrdbuf)
  344. char *wrdbuf;
  345. {
  346.     int i, j, k;
  347.     char s[MAXLINE], ch;
  348.     int line_len, outw, new_out_width, wid;
  349.     int nextra;
  350.  
  351.     if DEBUG fprintf(stderr,"\nwrdbuf = <%s>",wrdbuf);
  352.     skip_blanks (wrdbuf);
  353.     trunc_bl (wrdbuf);
  354.     wid = strlen (wrdbuf);
  355.     if DEBUG fprintf(stderr,"\nwid = %d",wid);
  356.  
  357.     line_len = RMVAL - TIVAL;
  358.     outw = strlen (OUTBUF);
  359.     new_out_width = wid + outw + 1;    /* one for blank */
  360.     if DEBUG fprintf(stderr,"\nnew_out_width=%d, outw=%d, wid=%d, line_len=%d",
  361.       new_out_width,    outw,    wid,    line_len   );
  362.     if (new_out_width > min(line_len, MAXLINE-1))
  363.     { nextra = min(line_len, MAXLINE-1) -outw + 1;
  364.         spread (OUTBUF, nextra, OUTWRDS);
  365.         brk();
  366.     }
  367.  
  368.     strcat (OUTBUF, wrdbuf);
  369.     strcat (OUTBUF, " ");
  370.     if DEBUG fprintf(stderr,"\nPUTWRD:OUTBUF=<%s>",OUTBUF);
  371.     OUTWRDS++;
  372.     return;
  373. }
  374.  
  375. /**************************************************************
  376.     returns no. of ch in string
  377. ***************************************************************/
  378. int count_char (string, ch)
  379. char *string, ch;
  380. {
  381.     int n;
  382.     char *p;
  383.  
  384.     p = string;
  385.     n=0;
  386.     while (*p)    if (*p++ == ch)    n++;
  387.     return (n);
  388. }
  389.  
  390. /***********************************************************
  391.     remove all occurrences of ch; first, last, and no.
  392.     of occurrences returned in parameters
  393. ************************************************************/
  394. remove_char (string, ch, first, last, n_ch)
  395. char *string, ch;
  396. int *first, *last, *n_ch;
  397. {
  398.     char *p1, *p2;
  399.     find_char (string, ch, first, last);
  400.     *n_ch = count_char (string, ch);
  401.     p1=p2=string;
  402.     while (*p1 = *p2++)    if (*p1 != ch)    p1++;
  403.     return;
  404. }
  405.  
  406. /**************************************************************
  407.     a new putline routine; sends line to current output
  408. ***************************************************************/
  409. putline (line)
  410. char *line;
  411. {
  412.     int i;
  413.  
  414.     if (ULVAL >=0)
  415.     { replace_char (line, NEWLINE, CR);
  416.         fputs (line, stdout);
  417.         indent (TIVAL);
  418.         for (i=0; line[i] != '\0'; i++)
  419.             if (line[i] & 0x80)    putchar(UNDERLINE);
  420.         else putchar(line[i]);
  421.         putchar(CR);
  422.         putchar(NEWLINE);
  423.         if (ULVAL==0)    ULVAL--;
  424.     }
  425.     else fputs (line, stdout);
  426.     return;
  427. }
  428.